package com.yrd.fssync.webui.servlet;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.util.Date;
import java.util.Iterator;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.filefilter.FileFilterUtils;
import org.apache.commons.io.filefilter.IOFileFilter;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;

import com.alibaba.fastjson.JSON;
import com.yrd.common.webui.HttpEngine;
import com.yrd.common.webui.ResponseMessage;
import com.yrd.fssync.core.FileSystemMonitor;
import com.yrd.fssync.core.FileSystemMonitorManager;
import com.yrd.fssync.core.SyncRequest;
import com.yrd.fssync.core.Utils;

public class Operate {

	
	public void test(HttpServletRequest request, HttpServletResponse reponse) throws UnsupportedEncodingException, IOException{
		String name = request.getParameter("name");
		HttpEngine.getInstance().sendMessage("收到:"+name);
		reponse.getOutputStream().write(("收到"+name).getBytes("utf-8"));
		
	}
	
	public synchronized void sync(HttpServletRequest request, HttpServletResponse reponse) throws UnsupportedEncodingException, IOException, ParseException{
		String modifyTimeStr = request.getParameter("time");
		String listenerId = request.getParameter("listener");
		String syncerId = request.getParameter("syncer");
		Date modifyDate=null;
		if(StringUtils.isNotEmpty(modifyTimeStr)){
			modifyDate = DateUtils.parseDate(modifyTimeStr, "yyyy-MM-dd HH:mm:ss");
		}
		
		FileSystemMonitorManager fileSystemMonitorManager = Utils.getContext().getFileSystemMonitorManager();
		if(StringUtils.isNotEmpty(listenerId)){
			FileSystemMonitor fileSystemMonitor  = fileSystemMonitorManager.get(listenerId);
			_sync(fileSystemMonitor,syncerId,modifyDate);
		}else{
			for(FileSystemMonitor fileSystemMonitor : fileSystemMonitorManager.getFileSystemMap().values()){
				_sync(fileSystemMonitor,syncerId,modifyDate);
			}
		}
		reponse.getOutputStream().write(JSON.toJSONString(ResponseMessage.success("OK")).getBytes("utf-8"));
	}
	private void _sync(FileSystemMonitor fileSystemMonitor, String syncerId,Date modifyDate) {
		File localBaseDir = new File(fileSystemMonitor.getLocalBase());
		IOFileFilter fileFilter = FileFilterUtils.fileFileFilter();
		IOFileFilter dirFilter = FileFilterUtils.directoryFileFilter();
		if(modifyDate!=null){
			fileFilter = FileFilterUtils.and(fileFilter,FileFilterUtils.ageFileFilter(modifyDate,false));
			dirFilter = FileFilterUtils.and(dirFilter,FileFilterUtils.ageFileFilter(modifyDate,false));
		}
		Iterator<File> fite= FileUtils.iterateFilesAndDirs(localBaseDir,fileFilter,dirFilter);
		while(fite.hasNext()){
			File file = fite.next();
			String fileRelativePath = Utils.relativeLocalPath(localBaseDir,file);
			if(file.isDirectory()){
				SyncRequest requestSync = new SyncRequest(file, fileRelativePath, SyncRequest.FILETYPE_DIR,SyncRequest.DISPOSE_OVERWRITE);
				fileSystemMonitor.post(requestSync,syncerId);
			}else if(file.isFile()){
				SyncRequest requestSync = new SyncRequest(file, fileRelativePath, SyncRequest.FILETYPE_FILE,SyncRequest.DISPOSE_OVERWRITE);
				fileSystemMonitor.post(requestSync,syncerId);
			}
		}
	}
	public synchronized void clear(HttpServletRequest request, HttpServletResponse reponse) throws UnsupportedEncodingException, IOException, ParseException{
		String listenerId = request.getParameter("listener");
		String syncerId = request.getParameter("syncer");
		
		FileSystemMonitorManager fileSystemMonitorManager = Utils.getContext().getFileSystemMonitorManager();
		if(StringUtils.isNotEmpty(listenerId)){
			FileSystemMonitor fileSystemMonitor  = fileSystemMonitorManager.get(listenerId);
			File localBaseDir = new File(fileSystemMonitor.getLocalBase());
			SyncRequest requestDelete = new SyncRequest(localBaseDir, null, SyncRequest.FILETYPE_DIR,SyncRequest.DISPOSE_DELETE);
			fileSystemMonitor.post(requestDelete,syncerId);
		}else{
			for(FileSystemMonitor fileSystemMonitor : fileSystemMonitorManager.getFileSystemMap().values()){
				File localBaseDir = new File(fileSystemMonitor.getLocalBase());
				SyncRequest requestDelete = new SyncRequest(localBaseDir, null, SyncRequest.FILETYPE_DIR,SyncRequest.DISPOSE_DELETE);
				fileSystemMonitor.post(requestDelete,syncerId);
			}
		}
		FileSystemMonitor fileSystemMonitor  = fileSystemMonitorManager.get(listenerId);
		reponse.getOutputStream().write(JSON.toJSONString(ResponseMessage.success("OK")).getBytes("utf-8"));
	}
	
	
}
